import { makeAutoObservable, observable, computed, action } from "mobx";

/**
 * 持久化key
 */
const key = 'RABBIT_PERSIST'
/**
 * 存储对象
 */
const storage = window.sessionStorage;

/**
 * 模块数据
 */
class rabbit {
  constructor() {
    makeAutoObservable(this);
    let data = storage.getItem(key);
    if (data) {
      data = JSON.parse(data);
      this.rabbit = data.rabbit;
     
    }
  }

  /**
   * 持久化
   */
  persist() {
    storage.setItem(key, JSON.stringify({
       rabbit:this.rabbit
      
    }));
  }

  /**
   * 登陆后的数据
   */
  @observable
  /**
   * 用户数据
   * **/
  rabbit = {};
  /**
   * 导航数据
   * **/
  navList=[]

  /**
   * token
   */
  @computed get token() {
    return this.rabbit.token;
  }

  /**
   * 设置数据
   */
  @action.bound setRabbit(value) {
    this.rabbit = value;
    this.persist();
  }

  /**
   * 导航数据
   * **/
   @action.bound setNavList(value) {
    this.navList = value;
    this.persist();
  }

  /**
   * 设置token
   */
  @action.bound setToken(token) {
    this.rabbit.token = token;
    this.persist();
  }

 
}

export default new rabbit();